<template>
  <div>
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="系统模块" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入系统模块"
          clearable
          size="default"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人员" prop="operName">
        <el-input
          v-model="queryParams.operName"
          placeholder="请输入操作人员"
          clearable
          size="default"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <template v-if="showAll">
        <el-form-item label="类型" prop="businessType">
          <el-select v-model="queryParams.businessType" placeholder="操作类型" clearable size="default">
            <el-option
              v-for="dict in dictType"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="操作状态" clearable size="default">
            <el-option label="成功" value="0">成功</el-option>
            <el-option label="失败" value="1">失败</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作时间">
          <el-date-picker
            v-model="dateRange"
            size="default"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
      </template>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="default" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="default" @click="resetQuery">重置</el-button>
        <el-button type="text" id="closeSearchBtn" @click="closeSearch">
          {{ word }}
          <i :class="showAll ? 'el-icon-arrow-up ' : 'el-icon-arrow-down'"></i>
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
           
          plain
          icon="el-icon-delete"
          size="default"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
          <!-- v-if="permissiones(['monitor:operlog:remove'])" -->
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-delete"
          size="default"
          @click="handleClean"
        >清空</el-button>
          <!-- v-if="permissiones(['monitor:operlog:remove'])" -->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="default"
          @click="handleExport"
        >导出</el-button>
          <!-- v-if="permissiones(['monitor:operlog:export'])" -->
      </el-col>
    </el-row>

    <el-table
    :header-cell-style="{background:'#fafafa'}"
    :border="true"
      v-loading="loading"
      ref="tables"
      :data="list"
      @selection-change="handleSelectionChange"
      :default-sort="defaultSort"
      @sort-change="handleSortChange"
      height="500"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <!-- <el-table-column label="日志编号" align="center" prop="operId"/> -->
      <el-table-column label="系统模块" align="center" prop="title"/>
      <el-table-column label="操作类型" align="center" prop="businessType">
        <template #default="scope">
          <el-tag
            v-for="(item, index) in dictType"
            v-show="scope.row.businessType == item.value"
            :key="index"
            :type="item.cssName"
          >{{ item.label }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="请求方式" align="center" prop="requestMethod"/>
      <el-table-column
        label="操作人员"
        align="center"
        prop="operName"
        width="100"
        :show-overflow-tooltip="true"
        sortable="custom"
        :sort-orders="['descending', 'ascending']"
      />
      <el-table-column
        label="操作地址"
        align="center"
        prop="operIp"
        width="130"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作地点"
        align="center"
        prop="operLocation"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作状态" align="center" prop="status">
        <template #default="scope">
          <el-tag :type="scope.row.status == 1 ? 'danger' : 'success'">
            {{
            scope.row.status == 1 ? "停用" : "正常"
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作日期"
        align="center"
        prop="operTime"
        sortable="custom"
        :sort-orders="['descending', 'ascending']"
        width="180"
      >
        <template #default="scope">
          <span>{{ scope.row.operTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="default"
            type="text"
            @click="handleView(scope.row, scope.index)"
          >详细</el-button>
            <!-- v-if="permissiones(['monitor:operlog:query'])" -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="pagination"
    />

    <!-- 操作日志详细 -->
    <el-dialog title="操作日志详细" v-model="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="default">
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作模块：">{{ form.title }} / {{ typeFormat(form) }}</el-form-item>
            <el-form-item label="登录信息：">
              {{ form.operName }} / {{ form.operIp }} /
              {{ form.operLocation }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求地址：">{{ form.operUrl }}</el-form-item>
            <el-form-item label="请求方式：">
              {{
              form.requestMethod
              }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作方法：">{{ form.method }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="请求参数：">{{ form.operParam }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="返回参数：">
              {{
              form.jsonResult
              }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作状态：">
              <div v-if="form.status === 0">正常</div>
              <div v-else-if="form.status === 1">失败</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作时间：">{{ form.operTime }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="异常信息：" v-if="form.status === 1">
              {{
              form.errorMsg
              }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="open = false">关 闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
//引入消息弹出框
import { ElMessageBox, ElMessage } from "element-plus";
import {
  list,
  delOperlog,
  cleanOperlog,
  operlogExport
} from "@/api/system/operlog.js";
import { hasPermi } from "@/utils/auth";
import { selectDictLabel } from "@/utils/index";
//分页
import Pagination from "@/components/Pagination/index.vue";
export default {
  name: "Operlog",
  dicts: ["sys_oper_type", "sys_common_status"],
  components: {
    Pagination
  },
  data() {
    return {
      showAll: true, //是否展开全部
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 默认排序
      defaultSort: { prop: "operTime", order: "descending" },
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        operName: undefined,
        businessType: undefined,
        status: undefined
      },
      dictType: [
        {
          value: "1",
          label: "新增",
          cssName: "success"
        },
        {
          value: "2",
          label: "修改",
          cssName: "parmary"
        },
        {
          value: "3",
          label: "删除",
          cssName: "danger"
        },
        {
          value: "4",
          label: "授权",
          cssName: "warning"
        },
        {
          value: "5",
          label: "导出",
          cssName: "info"
        },
        {
          value: "6",
          label: "导入",
          cssName: "warning"
        },
        {
          value: "7",
          label: "强退",
          cssName: "danger"
        },
        {
          value: "8",
          label: "生成代码",
          cssName: ""
        },
        {
          value: "9",
          label: "清空数据",
          cssName: "danger"
        }
      ]
    };
  },
  computed: {
    word: function() {
      if (this.showAll == false) {
        //对文字进行处理
        return "展开搜索";
      } else {
        return "收起搜索";
      }
    }
  },
  created() {
    this.getList();
  },
  mounted() {
    /**
     * 收起搜索
     */
    this.$nextTick(function() {
      this.closeSearch();
    });
  },
  methods: {
    /**展开收起 */
    closeSearch() {
      this.showAll = !this.showAll;
    },
    /**权限问题 */
    permissiones(value) {
      return hasPermi(value);
    },
    /**
     * 获取分页参数
     */
    pagination({ page, limit }) {
      this.queryParams.pageNum = page;
      this.queryParams.pageSize = limit;
      this.getList();
    },
    /** 查询登录日志 */
    getList() {
      this.loading = true;
      list(this.queryParams, this.dateRange)
        .then(response => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch(err => {
          this.$message.error(err.msg);
        });
    },
    // 操作日志类型字典翻译
   typeFormat(row, column) {
      return selectDictLabel(
        this.dictType,
        row.businessType
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.$refs.queryForm.resetFields();
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order);
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.operId);
      this.multiple = !selection.length;
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.open = true;
      this.form = row;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const operIds = row.operId || this.ids;
      ElMessageBox.confirm(
        '是否确认删除日志编号为"' + operIds + '"的数据项？',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delOperlog(operIds);
        })
        .then(() => {
          this.getList();
          ElMessage({
            type: "success",
            message: "删除成功"
          });
        })
        .catch(() => {
          this.$message.info("已取消");
        });
    },
    /** 清空按钮操作 */
    handleClean() {
      ElMessageBox.confirm("是否确认清空所有操作日志数据项？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return cleanOperlog();
        })
        .then(() => {
          this.getList();
          ElMessage({
            type: "success",
            message: "清空成功"
          });
        })
        .catch(() => {
          this.$message.info("已取消");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      operlogExport()
        .then(res => {
          const link = document.createElement("a");
          let blob = new Blob([res], {
            type: res.type
          });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          link.setAttribute("download", "操作日志统计");
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch(err => {
          this.$message.error("导出失败");
        });
    }
  }
};
</script>
<style lang="less" scoped></style>
